home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / pine / osdep / readfile < prev    next >
Text File  |  1995-11-22  |  944b  |  43 lines

  1. /*----------------------------------------------------------------------
  2.     Read whole file into memory
  3.  
  4.   Args: filename -- path name of file to read
  5.  
  6.   Result: Returns pointer to malloced memory with the contents of the file
  7.           or NULL
  8.  
  9. This won't work very well if the file has NULLs in it and is mostly
  10. intended for fairly small text files.
  11.  ----*/
  12. char *
  13. read_file(filename)
  14.     char *filename;
  15. {
  16.     int         fd;
  17.     struct stat statbuf;
  18.     char       *buf;
  19.     int         nb;
  20.  
  21.     fd = open(filename, O_RDONLY);
  22.     if(fd < 0)
  23.       return((char *)NULL);
  24.  
  25.     fstat(fd, &statbuf);
  26.  
  27.     buf = fs_get((size_t)statbuf.st_size + 1);
  28.  
  29.     /*
  30.      * On some systems might have to loop here, if one read isn't guaranteed
  31.      * to get the whole thing.
  32.      */
  33.     if((nb = read(fd, buf, (int)statbuf.st_size)) < 0)
  34.       fs_give((void **)&buf);        /* NULL's buf */
  35.     else
  36.       buf[nb] = '\0';
  37.  
  38.     close(fd);
  39.     return(buf);
  40. }
  41.  
  42.  
  43.